<?php
// 主页
namespace Admin\Controller;

use Think\Controller;
use Org\Net\UploadFile;
use Admin\Model\SiteModel;

class AjaxController extends CommonController {
	public function site() {
		$do = I ( 'get.do' );
		$id = I ( 'get.id' );
		$site = new SiteModel ();
		switch ($do) {
			case 'save' :
				$rs = $site->saveData ( $_POST, $id );
				break;
			case 'checkUrl' :
				$rs = $site->checkUrl ( I ( 'get.url' ), I ( 'get.id' ) );
				break;
			case 'getInfo' :
				$id = $id ? $id : I ( 'get.url' );
				$rs = $site->getInfo ( $id );
				break;
			case 'delete' :
				$id = $id ? $id : I ( 'get.url' );
				$rs = $site->delete ( $id );
				break;
			case 'search' :
				$page = I ( 'get.page' );
				$key = I ( 'get.key' );
				$where = 1;
				if ($key && $key != 'undefined') {
					$where .= ' and name like "%' . $key . '%"';
				}
				$order = 'id desc';
				$rs = $site->search ( $where, $page, $order );
				break;
			default :
				$rs = 0;
		}
		$this->ajaxReturn ( $rs );
	}
	public function upload() {
		die('停用此方法');
		$site = I ( 'post.site' );
		$dir = ($site ? $site : date ( 'Y' )) . '/';
		$category = I ( 'post.category' );
		$dir .= ($category ? $category : date ( 'm' )) . '/';
		$savePath = C ( 'upload_path' ) . '/' . $dir;
		$this->_mkDir ( $savePath );
		if ($site || $category) {
			$fileName = $site . ($category ? '-' . $category : '') . '-';
		}
		if ($category == 'thumb') {
			$fileName = 'thumb';
		} else {
			$fileName .= uniqid ();
		}
		$upload = new UploadFile (); // 实例化上传类
		$upload->maxSize = 1024 * 1024 * 2; // 设置附件上传大小
		$upload->allowExts = array (
				'jpg',
				'gif',
				'png',
				'jpeg'
		);
		// 设置附件上传类型
		$upload->savePath = $savePath; // 设置附件上传目录
		$upload->uploadReplace = true;
		$upload->saveRule = $fileName;
		if (! $upload->upload ()) { // 上传错误提示错误信息
			$data ['msg'] = $upload->getErrorMsg ();
			$data ['status'] = false;
		} else {
			// 上传成功 获取上传文件信息
			$info = $upload->getUploadFileInfo ();
			$data ['name'] = $info [0] ['name'];
			$path = $info [0] ['savepath'] . $info [0] ['savename'];
			$path500 = $info [0] ['savepath'] . str_replace ( '.', '_500_500.', $info [0] ['savename'] );
			$path150 = $info [0] ['savepath'] . str_replace ( '.', '_150_150.', $info [0] ['savename'] );

			// 缩略图
			$image = new \Think\Image ();
			$image->open ( $path );
			$image->thumb ( 150, 150, \Think\Image::IMAGE_THUMB_CENTER )->save ( $path150, null, true );
			$image->open ( $path );
			$image->thumb ( 500, 500, \Think\Image::IMAGE_THUMB_CENTER )->text ( 'hua.fengqiaoju.com 枫桥居花卉 ', './Public/msyh.ttc', 12, '#ffffff', \Think\Image::IMAGE_WATER_SOUTHEAST )->save ( $path500, null, true );
			$data ['status'] = true;
		}
		if (IS_AJAX) {
			$this->ajaxReturn ( $data );
		} else {
			echo '<script>parent.uploadCallBack("' . C ( 'img_server' ) . '","' . str_replace ( C ( 'upload_path' ), '', $path ) . '");</script>';
		}
	}
}